﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Serialization;

namespace Gui
{
    public partial class Form1 : Form
    {
        private RPPedal rp360Fxs;
        private Regex xmlns;
        private RP500Preset xmlPreset;

        public Form1()
        {
            InitializeComponent();

            xmlns = new Regex ("xmlns=\\\"[\\w:/\\.]+\\\"");
            try
            {
                XmlSerializer xs = new XmlSerializer(typeof(RPPedal));
                rp360Fxs = (RPPedal)xs.Deserialize(new MemoryStream(Encoding.Default.GetBytes(File.ReadAllText("RP360.xml"))));
            }
            catch (Exception ex) { MessageBox.Show("Error loading xml : " + ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string content = null;

                try
                {
                    content = File.ReadAllText(openFileDialog1.FileName);
                    richTextBox1.Text = content;
                }
                catch (Exception ex) { MessageBox.Show("Error loading preset : "+ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); }

                XmlSerializer xs = new XmlSerializer(typeof(RP500Preset));
                try
                {
                    // .NET xmlserializer throws exception if we leave xmlns attribute, so we'll cut it off
                    Match m = xmlns.Match(content);
                    if (m.Success) content = content.Remove(m.Index, m.Length);

                    xmlPreset = (RP500Preset)xs.Deserialize(new MemoryStream(Encoding.Default.GetBytes(content)));
                }
                catch (Exception ex) { MessageBox.Show("Error parsing preset : " + ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); }
            }
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            // conversion
            if (null != xmlPreset)
            {
                int current = -1;
                List<RP360Pedal> _pedals = new List<RP360Pedal>();
                RP360Pedal pedal = null;

                for (int i = 0; i < xmlPreset.params_.Length - 1; i++)
                {
                    if (current != xmlPreset.params_[i].Position)
                    {
                        // begin
                        current = xmlPreset.params_[i].Position;

                        if (null != pedal)
                        {
                            pedal.Build(rp360Fxs);
                            _pedals.Add(pedal);
                        }

                        pedal = new RP360Pedal();
                        pedal.AddParam(xmlPreset.params_[i]);
                    }
                    else
                    {
                        pedal.AddParam(xmlPreset.params_[i]);
                    }
                }
            }
            else
            {
                MessageBox.Show("No preset loaded.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
